package com.itfreer.power.dao.role;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.itfreer.form.api.JdbcBaseDaoImp;
import com.itfreer.power.dao.user.JdbcUserRoleEntity;
import com.itfreer.utils.hibernate.PredicateConvertUtils;
import com.itfreer.utils.hibernate.QueryFieldsConvertUtils;
import com.itfreer.power.entity.role.RoleDataJurisdictionEntity;
import com.itfreer.power.entity.role.RoleEntity;
import com.itfreer.power.entity.role.RoleMapExtentJurisdictionEntity;
import com.itfreer.power.entity.role.RoleMapLayerJurisdictionEntity;
import com.itfreer.power.entity.role.RoleMenuJurisdictionEntity;
import com.itfreer.power.entity.role.RoleServiceJurisdictionEntity;
import com.itfreer.power.entity.user.UserRoleEntity;

/**
 * 定义用户信息持久化操作类
 */
@Transactional(readOnly = false)
@Component("jdbcRoleDao")
public class JdbcRoleDao extends JdbcBaseDaoImp<RoleEntity, JdbcRoleEntity> implements RoleDao {

	/**
	 * 删除用户角色关系
	 * 
	 * @param userId
	 * @param roleId
	 */
	public void delUserRole(String userId, String roleId) {
		if (userId == null || userId.trim().equals("")) {
			return;
		}
		if (roleId == null || roleId.trim().equals("")) {
			return;
		}

		Session session = this.sessionFactory.getCurrentSession();
		@SuppressWarnings("deprecation")
		Criteria crit = session.createCriteria(JdbcUserRoleEntity.class);
		crit.add(Restrictions.eq("userId", userId));
		crit.add(Restrictions.eq("roleId", roleId));
		QueryFieldsConvertUtils.convert(crit, "id", JdbcUserRoleEntity.class);

		JdbcUserRoleEntity entity = (JdbcUserRoleEntity) crit.uniqueResult();
		if (entity != null) {
			session.delete(entity);
		}
	}

	/**
	 * 添加用户角色关系
	 * 
	 * @param userId
	 * @param roleId
	 */
	public void addUserRole(String userId, String roleId) {
		JdbcUserRoleEntity entity = new JdbcUserRoleEntity();
		entity.setRoleId(roleId);
		entity.setUserId(userId);
		String id = UUID.randomUUID().toString().replace("-", "");
		entity.setId(id);
		Session session = this.sessionFactory.getCurrentSession();
		session.save(entity);
	}

	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public List<UserRoleEntity> getRoles(String userid) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcUserRoleEntity.class);

		// 格式化查询条件
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("userId", userid);
		PredicateConvertUtils.convert(crit, where);

		crit.setFirstResult(0);
		crit.setMaxResults(100000);
		List<JdbcUserRoleEntity> list = crit.list();

		List<UserRoleEntity> jEntityList = new ArrayList<UserRoleEntity>();
		if (list == null || list.size() <= 0) {
			return jEntityList;
		}
		for (JdbcUserRoleEntity entity : list) {
			if (entity == null) {
				continue;
			}
			jEntityList.add(entity.toEntity());
		}
		return jEntityList;
	}

	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public List<RoleServiceJurisdictionEntity> getServices(String roles) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcRoleServiceJurisdictionEntity.class);

		// 格式化查询条件
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("roleId", roles);
		PredicateConvertUtils.convert(crit, where);

		crit.setFirstResult(0);
		crit.setMaxResults(100000);
		List<JdbcRoleServiceJurisdictionEntity> list = crit.list();

		List<RoleServiceJurisdictionEntity> jEntityList = new ArrayList<RoleServiceJurisdictionEntity>();
		if (list == null || list.size() <= 0) {
			return jEntityList;
		}
		for (JdbcRoleServiceJurisdictionEntity entity : list) {
			if (entity == null) {
				continue;
			}
			jEntityList.add(entity.toEntity());
		}
		return jEntityList;
	}
	
	/**
	 * 获取用户与菜单服务关联信息
	 * @param roleId
	 * @returns
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public List<RoleMenuJurisdictionEntity> getMenus(String roleId) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcRoleMenuJurisdictionEntity.class);

		// 格式化查询条件
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("roleId", roleId);
		PredicateConvertUtils.convert(crit, where);

		crit.setFirstResult(0);
		crit.setMaxResults(100000);
		List<JdbcRoleMenuJurisdictionEntity> list = crit.list();

		List<RoleMenuJurisdictionEntity> jEntityList = new ArrayList<RoleMenuJurisdictionEntity>();
		if (list == null || list.size() <= 0) {
			return jEntityList;
		}
		for (JdbcRoleMenuJurisdictionEntity entity : list) {
			if (entity == null) {
				continue;
			}
			jEntityList.add(entity.toEntity());
		}
		return jEntityList;
	}
	
	/**
	 * 获取用户与图层服务关联信息
	 * @param roleId
	 * @returns
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public List<RoleMapLayerJurisdictionEntity> getMapLayers(String roleId) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcRoleMapLayerJurisdictionEntity.class);

		// 格式化查询条件
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("roleId", roleId);
		PredicateConvertUtils.convert(crit, where);

		crit.setFirstResult(0);
		crit.setMaxResults(100000);
		List<JdbcRoleMapLayerJurisdictionEntity> list = crit.list();

		List<RoleMapLayerJurisdictionEntity> jEntityList = new ArrayList<RoleMapLayerJurisdictionEntity>();
		if (list == null || list.size() <= 0) {
			return jEntityList;
		}
		for (JdbcRoleMapLayerJurisdictionEntity entity : list) {
			if (entity == null) {
				continue;
			}
			jEntityList.add(entity.toEntity());
		}
		return jEntityList;
	}
	
	/**
	 * 获取用户与地图范围服务关联信息
	 * @param roleId
	 * @returns
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public List<RoleMapExtentJurisdictionEntity> getMapExtents(String roleId) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcRoleMapExtentJurisdictionEntity.class);

		// 格式化查询条件
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("roleId", roleId);
		PredicateConvertUtils.convert(crit, where);

		crit.setFirstResult(0);
		crit.setMaxResults(100000);
		List<JdbcRoleMapExtentJurisdictionEntity> list = crit.list();

		List<RoleMapExtentJurisdictionEntity> jEntityList = new ArrayList<RoleMapExtentJurisdictionEntity>();
		if (list == null || list.size() <= 0) {
			return jEntityList;
		}
		for (JdbcRoleMapExtentJurisdictionEntity entity : list) {
			if (entity == null) {
				continue;
			}
			jEntityList.add(entity.toEntity());
		}
		return jEntityList;
	}
	
	/**
	 * 获取用户与数据服务关联信息
	 * @param roleId
	 * @returns
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public List<RoleDataJurisdictionEntity> getDatas(String roleId) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcRoleDataJurisdictionEntity.class);

		// 格式化查询条件
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("roleId", roleId);
		PredicateConvertUtils.convert(crit, where);

		crit.setFirstResult(0);
		crit.setMaxResults(100000);
		List<JdbcRoleDataJurisdictionEntity> list = crit.list();

		List<RoleDataJurisdictionEntity> jEntityList = new ArrayList<RoleDataJurisdictionEntity>();
		if (list == null || list.size() <= 0) {
			return jEntityList;
		}
		for (JdbcRoleDataJurisdictionEntity entity : list) {
			if (entity == null) {
				continue;
			}
			jEntityList.add(entity.toEntity());
		}
		return jEntityList;
	}
}
